﻿#include "kcftracker.hpp"
#include "boxExtractor.h"
#include <unistd.h>
//#pragma comment(lib, "opencv_world310d.lib")

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
	bool HOG = true;
	bool FIXEDWINDOW = false;
	bool MULTISCALE = true;
	bool SILENT = true;
	bool LAB = false;

	// Create KCFTracker object
	KCFTracker tracker(HOG, FIXEDWINDOW, MULTISCALE, LAB);

	// ROI selector
	BoxExtractor box;

	// set input video
	std::string video = "/home/linux/Videos/dataset/running.mp4";//"E://data//trackingData//tracking_clip.mp4";// "E://dudek.webm";//argv[1];
	VideoCapture cap;
	cap.open(video);	

	Mat frame;
	Mat frameSacle;
	Rect2d roi;
	Rect result;
	bool is_tracking = false;
	// get bounding box
	while (1)
	{
		// sleep()
		
		if(cap.isOpened())
		{
			cap >> frame;

			// imshow("frame",frame);
		}
		else
		{
			cout << "cap is not open" << endl;
			break;
		}	
		char key = (char)cvWaitKey(30);
		if (key == 't')
		{
			roi = box.extract("Image", frameSacle);
			if (roi.width == 0 || roi.height == 0)
				cout << "roi is not select" << endl;
			else
			{
				tracker.init(roi, frame);
				rectangle(frame, roi, Scalar(0, 255, 255), 1, 8);
				is_tracking = true;
			}			
		}
		if (is_tracking == true)
		{
			if (frame.rows == 0 || frame.cols == 0)
			{
				cout << "frame is error" << endl;
				continue;
			}
			else
			{
				result = tracker.update(frame);
				rectangle(frame, Point(result.x, result.y), Point(result.x + result.width, result.y + result.height), Scalar(0, 255, 255), 1, 8);
			}		
		}
		imshow("Image", frame);
		resize(frame,frameSacle,Size(frame.cols/2,frame.rows/2),0,0);
		if (key == 'q')
			break;

	}
	
	// while (1)
	// {
	// 	if(cap.isOpened())
	// 	{
	// 		cap >> frame;
	// 		sleep(1);
	// 		cap >> frame;
	// 		sleep(1);
	// 		cap >> frame;
	// 		roi = box.extract("tracker", frame);

	// 		if (roi.width == 0 || roi.height == 0)
	// 			return 0;

	// 		break;
	// 	}
	// 	cout << "cap is not open" << endl;
	// }

	//quit if ROI was not selected


	//printf("x = %f, y = %f, width = %f, height = %f;\n",roi.x,roi.y,roi.width,roi.height);/////输出长宽  从上往下框  从下往上框  区别

	// tracker.init(roi, frame);
	// rectangle(frame, roi, Scalar(0, 255, 255), 1, 8);
	// Rect result;
	// do the tracking
	// printf("Start the tracking process, press ESC to quit.\n");
	// while (1) 
	// {
	// 	// get frame from the video
	// 	cap >> frame;
	// 	// stop the program if no more images
	// 	if (frame.rows == 0 || frame.cols == 0)
	// 		break;
	// 	result = tracker.update(frame);


	// 	rectangle(frame, Point(result.x, result.y), Point(result.x + result.width, result.y + result.height), Scalar(0, 255, 255), 1, 8);
	// 	imshow("Image", frame);
	// 	//quit on ESC button
	// 	if (waitKey(1) == 27)break;
	// }


}



